package org.ws.lifebacklog.view;

import java.util.ArrayList;
import java.util.List;

import org.ws.lifebacklog.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author alexander.sokolovsky.a@gmail.com
 */
public class IndicatorView extends View {
	private List<Indicator> mIndicators = new ArrayList<Indicator>();

	private final int mBubbleBorderColor;
	private final float mTextSize;
	private final float mIndicatorPadding;
	private final float mBubbleOverlap;
	private final float mBubbleBorderWidth;
	private final float mBubblePaddingVertical;
	private final float mBubblePaddingHorizontal;
	private final float mTextStartDivisor;
	private final float mRadius;

	public IndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mBubbleBorderColor = getResources().getColor(R.color.indicator_border_color);
		mTextSize = getResources().getDimension(R.dimen.indicator_text_size);
		mIndicatorPadding = getResources().getDimension(R.dimen.indicator_padding);
		mBubbleOverlap = getResources().getDimension(R.dimen.indicator_bubble_overlap);
		mBubbleBorderWidth = getResources().getDimension(R.dimen.indicator_bubble_border_width);
		mBubblePaddingVertical = getResources().getDimension(R.dimen.indicator_bubble_padding_vertical);
		mBubblePaddingHorizontal = getResources().getDimension(R.dimen.indicator_bubble_padding_horizontal);
		mTextStartDivisor = (float) getResources().getInteger(R.integer.indicator_text_start_divisor) / 100;
		mRadius = getResources().getDimension(R.dimen.indicator_bubble_radius);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(mTextSize);
		paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

		paint.setStyle(Paint.Style.FILL);
		float position = 0;
		int counter = 1;
		if (mIndicators != null) {
			for (Indicator indicator : mIndicators) {
				boolean last = (counter == mIndicators.size());
				position = drawIndicator(position, indicator, last, paint, canvas);
				counter++;
			}
		}
	}

	private float drawIndicator(float position, Indicator indicator, boolean last, Paint paint, Canvas canvas) {
		float indicatorRectWidth = paint.measureText(Integer.toString(indicator.getNumber()))
				+ mBubblePaddingHorizontal;

		float textHeight = getTextHeight(paint);
		float indicatorRectHeight = textHeight + mBubblePaddingVertical;
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);

		// Adjust canvas
		canvas.translate(position, 0);

		RectF bubbleRect = new RectF(0, 0, indicatorRectWidth, indicatorRectHeight);
		RectF borderRect = new RectF(0, 0, indicatorRectWidth + (2 * mBubbleBorderWidth), indicatorRectHeight
				+ (2 * mBubbleBorderWidth));

		paint.setColor(mBubbleBorderColor);
		canvas.drawRoundRect(borderRect, mRadius, mRadius, paint);
		canvas.translate(mBubbleBorderWidth, mBubbleBorderWidth);
		paint.setColor(indicator.getColor());
		canvas.drawRoundRect(bubbleRect, mRadius, mRadius, paint);

		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.STROKE);
		paint.setTextAlign(Paint.Align.CENTER);
		// Center text if last, else move closer to edge
		float textStart = last ? indicatorRectWidth / 2 : indicatorRectWidth / mTextStartDivisor;
		canvas.drawText(Integer.toString(indicator.getNumber()), textStart, textHeight, paint);
		canvas.translate(0, -mBubbleBorderWidth);
		return indicatorRectWidth - mBubbleOverlap + mBubbleBorderWidth;
	}

	public void setIndicators(List<Indicator> indicators) {
		mIndicators = indicators;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		Paint paint = new Paint();
		paint.setTextSize(mTextSize);
		paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		float textHeight = getTextHeight(paint) + mBubblePaddingVertical + (2 * mIndicatorPadding);
		setMeasuredDimension(getBubbleWidth(paint), (int) textHeight);
	}

	private int getBubbleWidth(Paint paint) {
		int width = 0;
		width += mIndicatorPadding;
		for (Indicator notification : mIndicators) {
			float textWidth = paint.measureText(Integer.toString(notification.getNumber()));
			width += (textWidth) + mBubblePaddingHorizontal - mBubbleOverlap + (2 * mBubbleBorderWidth);
		}
		width += mBubbleOverlap + mIndicatorPadding;
		return width;
	}

	private float getTextHeight(Paint paint) {
		return -(paint.getFontMetrics().top);
	}

	public static class Indicator {
		private int color;
		private int number;

		public Indicator(int color, int number) {
			this.color = color;
			this.number = number;
		}

		public int getColor() {
			return color;
		}

		public void setColor(int color) {
			this.color = color;
		}

		public int getNumber() {
			return number;
		}

		public void setNumber(int number) {
			this.number = number;
		}
	}

}
